package cn.lopr.im.android.mvp.view.activities;

import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import cn.lopr.im.android.R;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.databinding.ActivityAddFriendBinding;
import cn.lopr.im.android.db.entity.Message;
import cn.lopr.im.android.db.service.MessageDaoService;
import cn.lopr.im.android.mvp.contact.IAddFriendContact;
import cn.lopr.im.android.mvp.presenter.AddFriendPresenterImpl;
import cn.lopr.im.android.mvp.view.adapters.AddFriendAdapter;

public class AddFriendActivity extends Activity implements IAddFriendContact.IAddFriendView, AddFriendAdapter.OnItemListen {

    ActivityAddFriendBinding binding;
    private AddFriendPresenterImpl addFriendPresenterImpl;
    private AddFriendAdapter addFriendAdapter;
    private String actionSearch = "search";
    private String actionApply = "apply";
    private String currentAction;

    @Override
    protected View getContentLayoutId() {
        binding = ActivityAddFriendBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        this.binding.includeInput.tvSend.setOnClickListener(e -> addFriendPresenterImpl.searchFriend());
        this.binding.includeInput.tvSend.setText("搜索");
        this.binding.includeInput.etContent.setHint("   请输入对方手机号...");
        this.binding.includeAppbar.ivBack.setOnClickListener(e -> finish());
        this.binding.includeInput.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                addFriendPresenterImpl.searchFriend();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        this.currentAction = getIntent().getStringExtra("action");
        this.addFriendAdapter = new AddFriendAdapter(this.getApplicationContext(), this.currentAction);
        this.addFriendAdapter.setOnItemListen(this);
        this.binding.lvUser.setAdapter(this.addFriendAdapter);
        this.addFriendPresenterImpl = new AddFriendPresenterImpl(this, this);
        if (this.currentAction.equals(actionApply)) {
            this.binding.includeInput.cl.setVisibility(View.GONE);
            this.addFriendPresenterImpl.getAddFriendApply();
            this.binding.includeAppbar.tvTitle.setText("好友申请");
        } else {
            binding.includeEmpty.rlEmpty.setVisibility(View.VISIBLE);
            binding.includeEmpty.tvEmpty.setText("开始搜索好友");
        }
    }

    @Override
    public void searchFriendSuccess(List<User> users) {
        if (users == null || users.size() == 0) {
            if (actionApply.equals(currentAction)) {
                binding.includeEmpty.rlEmpty.setVisibility(View.VISIBLE);
                binding.includeEmpty.tvEmpty.setText("暂时没有好友申请");
            }
        } else {
            binding.lvUser.setVisibility(View.VISIBLE);
            binding.includeEmpty.rlEmpty.setVisibility(View.GONE);
        }
        this.addFriendAdapter.refer(users);
    }

    @Override
    public void addFriendSuccess(String message) {
        addFriendPresenterImpl.getAddFriendApply();
        Toast.makeText(AddFriendActivity.this, message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void getAddFriendApplySuccess(List<Message> messages) {

    }

    @Override
    public String getSearchContent() {
        return binding.includeInput.etContent.getText().toString();
    }

    @Override
    public void onClick(View view, int i) {
        User user = (User) view.getTag();
        if (view.getId() == R.id.tv_send) {
            // 发送加好友信息
            if (actionSearch.equals(currentAction)) {
                this.addFriendPresenterImpl.sendMessage(user.getPhone());
            } else {
                this.addFriendPresenterImpl.addFriend(user.getPhone());
            }
        }
    }

    @Override
    public void onLongClick(View view, int i) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        currentAction = "";
    }
}